package com.enterprise.support.servlet;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;

import com.enterprise.support.utility.Enterprise;

import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Configuration;
import freemarker.template.TemplateHashModel;

/**
 * *******************************************************************************************************************
 * Copyright (c) 2010   :<上海八和科技>
 * Project              :<>
 * Comments             :<>
 * JDK version used:    :<JDK1.6>
 * Namespace            :<com.enterprise.support.servlet>
 * Author               :<ZhangGaoJiang>
 * Create Date          :<2010-10-12 10:26:47>
 * <p/>
 * ********************************************************************************************************************
 */
public class FreemarkerServlet extends freemarker.ext.servlet.FreemarkerServlet {
    private Configuration config;

    public void init() throws ServletException {
        super.init();
        config = getConfiguration();
        /*异常*/
        config.setTemplateExceptionHandler(new com.enterprise.application.mvc.struts.view.freemarker.TemplateExceptionHandler());

        try {
            //设置 在freemarker内可以调用到的 函数
            config.setSharedVariable("statics", BeansWrapper.getDefaultInstance().getStaticModels());

            Enterprise<String, Object> templates = new Enterprise(com.enterprise.support.utility.Configuration.getProperties("/config/freemarker/template.properties"));
            Map<String, Object> values = new HashMap();
            for (String key : templates.keySet()) {
                getTemplates(values, 0, key.split("\\."), templates.getString(key));
            }
            for (String key : values.keySet()) {
                config.setSharedVariable(key, values.get(key));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    /**
     * 递归形成 树状结构
     */
    public static Map getTemplates(Map values, int index, String[] keys, String value) {
        if (values == null) values = new HashMap();
        if (index < keys.length - 1) {
            if (!values.containsKey(keys[index])) {
                values.put(keys[index], new HashMap());
            }
            getTemplates((Map) values.get(keys[index]), index + 1, keys, value);
        } else {
            try {
                values.put(keys[index], Class.forName(value).newInstance());
            } catch (Exception ex) {
                TemplateHashModel staticModels = BeansWrapper.getDefaultInstance().getStaticModels();
                try {
                    values.put(keys[index], staticModels.get(value));
                } catch (Exception ex2) {
                    ex2.printStackTrace(System.err);
                }
            }
        }
        return values;
    }

    public static void main(String[] args) {
        Map<String, Object> values = getTemplates(new HashMap(), 0, "srcsky.common.category".split("\\."), "com.srcsky.application.common.category.logic.impl.CategoryImpl");
        System.out.println();
        for (String key : values.keySet()) {
            System.out.println(key + ":" + values.get(key));
        }
    }
}
